package com.foo.common.base.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.foo.common.base.component.BaseDataSourceCleanComponent;
import com.foo.common.base.utils.FooUtils;
//import com.mysql.jdbc.AbandonedConnectionCleanupThread;

import java.sql.DriverManager;

public enum BaseDataSourceCleanImpl implements BaseDataSourceCleanComponent {

    INSTANCE;

    private final org.slf4j.Logger logger = org.slf4j.LoggerFactory
            .getLogger(BaseDataSourceCleanImpl.class);

    @Override
    public boolean isRunning() {
        return true;
    }

    @Override
    public void start() {
        logger.info("BaseDataSourceClean component start");
    }

    @Override
    public void stop() {

        try {
            logger.info("mysql:AbandonedConnectionCleanupThread clean.");
//            AbandonedConnectionCleanupThread.shutdown();

            DruidDataSource myDs = FooUtils
                    .getBean(DruidDataSource.class);
            myDs.close();

            while (DriverManager.getDrivers().hasMoreElements()) {
                DriverManager.deregisterDriver(DriverManager.getDrivers()
                        .nextElement());
            }

        } catch (Exception e) {
            logger.info("mysql:shutdown exception:{}", e);
        }

    }
}
